package com.zx.cashiersystem.service;

import com.vilyever.socketclient.SocketClient;

/**
 * Created by stone on 2017/1/11.
 */

public class ClientSockets{
    private static final String TAG = "ClientSockets";
    private static final String SERVERIP = "139.199.181.119";
    private static final String HEART_SEND = "{\"type\":\"pong\"}";
    private static final String HEART_RECEIVER = "{\"type\":\"ping\"}";
    private static final int PORT = 8272;
    private SocketClient socketClient;

    public ClientSockets() {
        this.configParams();
    }

    public void configParams(){
        socketClient = new SocketClient(SERVERIP, PORT);
        socketClient.disableHeartBeat();
        socketClient.setConnectionTimeout(1000 * 5);
        socketClient.setHeartBeatInterval(3000);
        socketClient.setHeartBeatMessage("{\"type\":\"pong\"}","UTF-8");
        socketClient.setRemoteNoReplyAliveTimeout(1000 * 15);
        socketClient.setCharsetName("UTF-8");
    }

    /*
    注册连接状态
    * */
    public void registerConnectListener(SocketClient.SocketDelegate l){
        socketClient.registerSocketDelegate(l);
    }
    /*
    移除注册连接状态
    * */
    public void unregisterConnectListener(SocketClient.SocketDelegate l){
        socketClient.removeSocketDelegate(l);
    }
    public void connect(){
        socketClient.connect();
    }
    public void disconnect(){
        socketClient.disconnect();
    }

    public void sendData(String str){
        socketClient.sendString(str+"\n");
    }
}
